package com.example.sorftware20220103.util;

import com.example.sorftware20220103.service.SuperService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;

import java.util.List;

@Component
public class PageInfoTool {
    public static final int pageNum = 1;
    public static int pageSize = 3;

    @Autowired
    private RedisTemplate redisTemplate;


    //目的就是为了实现面向接口编程的基类把接口当做参数进行的多态的处理
    //方法的重载专门针对返回页面使用
    public void setPageInfo(SuperService superService,
                            Object o,
                            Integer pagenum,
                            Model model) {

        if (pagenum == null) {
            pagenum = pageNum;
        }

        PageHelper.startPage(pagenum, pageSize);
        List<Object> list = superService.getList(o);
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("pageinfo", pageInfo);
        model.addAttribute("searchWhere", o);
    }

    //方法的重载专门针对返回对象使用
    public static PageInfo setPageInfo(SuperService superService,
                                       Object o,
                                       Integer pagenum) {
        if (pagenum == null) {
            pagenum = pageNum;
        }
        PageHelper.startPage(pagenum, pageSize);
        List<Object> list = superService.getList(o);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }
}
